package com.wxl.www.wxl.shiro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxl.www.wxl.shiro.entity.SysPlatform;
import com.wxl.www.wxl.shiro.entity.UserInfo;
import com.wxl.www.wxl.shiro.service.PlatformSystemService;
import com.wxl.www.wxl.shiro.utils.AOP.LoginUserInfo;
import com.wxl.www.wxl.shiro.utils.CreateUUID;
import com.wxl.www.wxl.shiro.utils.DataUtil;
import com.wxl.www.wxl.shiro.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 平台系统管理
 */
@Controller
@RequestMapping("/platform/systime")
public class PlatformController {

    @Autowired
    private PlatformSystemService platformSystemService;

    @RequestMapping("/list")
    public String list(
            @RequestParam(value="pageNo", defaultValue="1")Integer pageNo
            , @RequestParam(value="pageSize", defaultValue="10") Integer pageSize
            , Model model){
        IPage<SysPlatform> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SysPlatform> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_time");
        model.addAttribute("userinfoList",platformSystemService.page(page,wrapper));
        return "sys/platform/list";
    }
    @RequestMapping("/inster")
    public String toInster(){
        return "sys/platform/inster";
    }

    @PostMapping("/inster")
    public ResultUtil inster(SysPlatform sysPlatform,@LoginUserInfo() UserInfo userInfo){
        try {
            sysPlatform.setId(CreateUUID.getUUID32());
            sysPlatform.setCreateUser(userInfo.getId());
            sysPlatform.setCreateTime(DataUtil.getDate());
            sysPlatform.setUpdateUser(userInfo.getId());
            sysPlatform.setUpdateTime(DataUtil.getDate());
            boolean save = platformSystemService.save(sysPlatform);
            return new ResultUtil();
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }

    @RequestMapping("/update")
    public String toUpdate(String id,Model model){
        model.addAttribute("sysPlatform",platformSystemService.getById(id));
        return "sys/platform/update";
    }

    @PostMapping("/update")
    public ResultUtil update( SysPlatform sysPlatform,@LoginUserInfo() UserInfo userInfo){
            try {
                sysPlatform.setUpdateUser(userInfo.getId());
                sysPlatform.setUpdateTime(DataUtil.getDate());
                boolean save = platformSystemService.updateById(sysPlatform);
                return new ResultUtil();
            }catch (Exception e){
                System.out.println(e);
            }
            return null;
    }

    /**
     * 删除
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultUtil delete(String id){
        ResultUtil resultUtil = new ResultUtil();
        if(platformSystemService.removeById(id)){
            resultUtil.setCode(200);
        }
        return resultUtil;
    }
}
